package com.atguigu.gulimall.cart.vo;

import lombok.ToString;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @version 1.0
 * @Author yanjingren
 * @Date 2021/6/23
 * @Description com.atguigu.gulimall.cart.vo
 */

/**
 * 购物车
 * 需要计算的属性，需要重写get方法，保证每次获取属性，都会进行计算
 */
@ToString
public class Cart {

    private List<CartItem> items;

    private Integer countNum;  // 商品数量

    private Integer countType;  // 商品类型数量

    private BigDecimal totalAmount; // 商品总价

    private BigDecimal reduce = new BigDecimal("0.00");  // 减免价格

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        countNum = 0;
        if (!CollectionUtils.isEmpty(this.items)) {
            for (CartItem item : items) {
                countNum += item.getCount();
            }
        }
        return countNum;
    }

    public Integer getCountType() {
        if (!CollectionUtils.isEmpty(this.items)) {
            return items.size();
        }
        return 0;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        // 计算购物项总价
        if (!CollectionUtils.isEmpty(this.items)) {
            for (CartItem item : items) {
                if (item.getCheck()) {
                    BigDecimal totalPrice = item.getTotalPrice();
                    amount = amount.add(totalPrice);
                }
            }
        }
        // 减去优惠总价
        BigDecimal subtract = amount.subtract(getReduce());
        return subtract;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
